/******************************************************************************
 * FILE NAME:
 *  logging.c
 *
 * DESCRIPTION:
 * This file contains the functions logging.
 *
 * DESIGN REFERENCE:
 *
 *
 * REVISION HISTORY   :
 * Date            Author               REASON
 * 06.08.2013      Thuan Nguyen         Creation
 *
 * Copyright 2005, NET
 * ***************************************************************************/

#include"../h/logging.h"


/***********************************************************************
 * FUNCTION NAME:
 *  write_log_to_file
 * DESCRIPTION:
 *
 * INPUT:
 *	request_msg_t *req_msg
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void write_log_to_file(char* file_name,char* buffer){
	FILE *log_file;
	
	log_file = fopen(file_name,"a+");
	if(SYSLOG_NOK == log_file)
	{
		exit(EXIT_FAILURE);
	}
	fwrite(buffer,sizeof(char),strlen(buffer),log_file);
	fclose(log_file);
}
/***********************************************************************
 * FUNCTION NAME:
 *  printf_func
 * DESCRIPTION:
 *
 * INPUT:
 *	request_msg_t *req_msg
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void printf_func(U8 log,U16 module,U8 *file_name,U32 line_num,const U8 *format,...)
{
	U8 trace_buffer[MAX_CHARACTERS_IN_LINE+1];
	U8 trace_buffer_temp[MAX_CHARACTERS_IN_LINE+1];

	U8 *buffer = trace_buffer;
	U8 *buffer_temp = trace_buffer_temp;
	va_list ap;
	U16 length =0;
	U16 log_mask;	
	
	if(SYSLOG_NOK != format){
		va_start(ap,format);
		vsnprintf(buffer,120,format,ap);
		va_end(ap);
	}
	
	if(strlen(buffer) > MAX_CHARACTERS_IN_LINE)
		length = MAX_CHARACTERS_IN_LINE;
	else
		length = strlen(file_name) + 25 + strlen(buffer);

	
	U8 type_log[8];
	U8 *p_type_log = type_log;
	U8 file_log_name[100];	
	U8 *p_file_log_name = file_log_name;
	memset(type_log,0,sizeof(type_log));
	memset(file_log_name,0,sizeof(file_log_name));

	switch(log | module){
		case TRANSPORT_INFO:
			p_type_log = "INFO";
			p_file_log_name = "./log/log_transport.log";
			break;
		case TRANSPORT_WARNING:
			p_type_log = "WARN";
			p_file_log_name = "./log/log_transport.log";
			break;
		case TRANSPORT_ERROR:
			p_type_log = "ERROR";
			p_file_log_name = "./log/log_transport.log";
			break;
		case TRANSPORT_DEBUG:
			p_type_log = "DEBUG";
			p_file_log_name ="./log/log_transport.log";
			break;
		case TRANSPORT_DETAIL:
			p_type_log = "DETAIL";
			p_file_log_name ="./log/log_transport.log";
			break;

		case APPLICATION_INFO:
			p_type_log = "INFO";
			p_file_log_name = "./log/log_application.log";
			break;
		case APPLICATION_WARNING:
			p_type_log = "WARN";
			p_file_log_name = "./log/log_application.log";
			break;
		case APPLICATION_ERROR:
			p_type_log = "ERROR";
			p_file_log_name = "./log/log_application.log";
			break;
		case APPLICATION_DEBUG:
			p_type_log = "DEBUG";
			p_file_log_name = "./log/log_application.log";
			break;
		case APPLICATION_DETAIL:
			p_type_log = "DETAIL";
			p_file_log_name = "./log/log_application.log";
			break;

		case CONTENT_INFO:
			p_type_log = "INFO";
			p_file_log_name = "./log/log_content.log";
			break;
		case CONTENT_WARNING:
			p_type_log = "WARN";
			p_file_log_name = "./log/log_content.log";
			break;
		case CONTENT_ERROR:
			p_type_log = "ERROR";
			p_file_log_name = "./log/log_content.log";
			break;
		case CONTENT_DEBUG:
			p_type_log = "DEBUG";
			p_file_log_name = "./log/log_content.log";
			break;
		case CONTENT_DETAIL:
			p_type_log = "DETAIL";
			p_file_log_name = "./log/log_content.log";
			break;

		case COMMON_INFO:
			p_type_log = "INFO";
			p_file_log_name = "./log/log_common.log";
			break;
		case COMMON_WARNING:
			p_type_log = "WARN";
			p_file_log_name = "./log/log_common.log";
			break;
		case COMMON_ERROR:
			p_type_log = "ERROR";
			p_file_log_name = "./log/log_common.log";
			break;
		case COMMON_DEBUG:
			p_type_log = "DEBUG";
			p_file_log_name = "./log/log_common.log";
			break;
		case COMMON_DETAIL:
			p_type_log = "DETAIL";
			p_file_log_name = "./log/log_common.log";
			break;
		default:
			p_type_log = "DEBUG";
			p_file_log_name = "./log/log_transport.log";
			break;
	}	
	snprintf(buffer_temp,length,"[%s]%s:(%d) \"%s\" \n",p_type_log \
				,file_name,line_num,buffer);
	write_log_to_file(p_file_log_name,buffer_temp);
}

